package com.jlwl.houseservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jlwl.houseservice.entity.Service;
import com.jlwl.houseservice.entity.ServiceCategory;
import com.jlwl.houseservice.entity.ServiceImage;
import com.jlwl.houseservice.entity.ServiceTag;
import com.jlwl.houseservice.entity.ServiceTagRelation;
import com.jlwl.houseservice.mapper.ServiceImageMapper;
import com.jlwl.houseservice.mapper.ServiceMapper;
import com.jlwl.houseservice.mapper.ServiceTagRelationMapper;
import com.jlwl.houseservice.service.ServiceCategoryService;
import com.jlwl.houseservice.service.ServiceService;
import com.jlwl.houseservice.service.ServiceTagService;
import com.jlwl.houseservice.vo.ServiceDetailVO;
import com.jlwl.houseservice.vo.ServiceQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 服务Service实现类
 */
@Component
@org.springframework.stereotype.Service
public class ServiceServiceImpl extends ServiceImpl<ServiceMapper, Service> implements ServiceService {

    @Autowired
    private ServiceCategoryService serviceCategoryService;

    @Autowired
    private ServiceTagService serviceTagService;

    @Autowired
    private ServiceTagRelationMapper serviceTagRelationMapper;

    @Autowired
    private ServiceImageMapper serviceImageMapper;

    @Override
    public Page<Service> pageList(ServiceQueryVO queryVO) {
        LambdaQueryWrapper<Service> queryWrapper = new LambdaQueryWrapper<>();
        
        // 构建查询条件
        if (StringUtils.hasText(queryVO.getName())) {
            queryWrapper.like(Service::getName, queryVO.getName());
        }
        
        if (queryVO.getCategoryId() != null) {
            queryWrapper.eq(Service::getCategoryId, queryVO.getCategoryId());
        }
        
        if (queryVO.getMinPrice() != null) {
            queryWrapper.ge(Service::getPrice, queryVO.getMinPrice());
        }
        
        if (queryVO.getMaxPrice() != null) {
            queryWrapper.le(Service::getPrice, queryVO.getMaxPrice());
        }
        
        if (queryVO.getIsSpecial() != null) {
            queryWrapper.eq(Service::getIsSpecial, queryVO.getIsSpecial());
        }
        
        if (queryVO.getStatus() != null) {
            queryWrapper.eq(Service::getStatus, queryVO.getStatus());
        }
        
        // 默认按ID排序
        queryWrapper.orderByAsc(Service::getId);
        
        // 分页查询
        return page(new Page<>(queryVO.getPage(), queryVO.getSize()), queryWrapper);
    }

    @Override
    public ServiceDetailVO getServiceDetail(Integer id) {
        // 获取服务信息
        Service service = getById(id);
        if (service == null) {
            return null;
        }
        
        // 获取服务分类
        ServiceCategory category = serviceCategoryService.getById(service.getCategoryId());
        
        // 获取服务标签
        List<ServiceTag> tags = getServiceTags(id);
        
        // 获取服务图片
        List<String> images = getServiceImages(id);
        
        // 构建返回结果
        ServiceDetailVO detailVO = new ServiceDetailVO();
        detailVO.setService(service);
        detailVO.setCategory(category);
        detailVO.setTags(tags);
        detailVO.setImages(images);
        
        return detailVO;
    }

    @Override
    public List<Service> getSpecialServices() {
        return list(new LambdaQueryWrapper<Service>()
                .eq(Service::getIsSpecial, 1)
                .eq(Service::getStatus, 1)
                .orderByDesc(Service::getSalesCount)
                .last("LIMIT 10"));
    }

    @Override
    public List<Service> getServicesByCategory(Integer categoryId) {
        return list(new LambdaQueryWrapper<Service>()
                .eq(Service::getCategoryId, categoryId)
                .eq(Service::getStatus, 1)
                .orderByDesc(Service::getSalesCount));
    }

    @Override
    public boolean updateServiceRating(Integer serviceId, BigDecimal rating) {
        return false;
    }

    /**
     * 获取服务标签
     */
    private List<ServiceTag> getServiceTags(Integer serviceId) {
        // 查询服务标签关联
        List<ServiceTagRelation> relations = serviceTagRelationMapper.selectList(
                new LambdaQueryWrapper<ServiceTagRelation>()
                        .eq(ServiceTagRelation::getServiceId, serviceId)
        );
        
        if (relations.isEmpty()) {
            return new ArrayList<>();
        }
        
        // 获取标签ID列表
        List<Integer> tagIds = relations.stream()
                .map(ServiceTagRelation::getTagId)
                .collect(Collectors.toList());
        
        // 查询标签信息
        return serviceTagService.listByIds(tagIds);
    }

    /**
     * 获取服务图片
     */
    private List<String> getServiceImages(Integer serviceId) {
        List<ServiceImage> images = serviceImageMapper.selectList(
                new LambdaQueryWrapper<ServiceImage>()
                        .eq(ServiceImage::getServiceId, serviceId)
                        .orderByAsc(ServiceImage::getDisplayOrder)
        );
        
        return images.stream()
                .map(ServiceImage::getImageUrl)
                .collect(Collectors.toList());
    }
} 